from flask_restful import Resource, reqparse, abort
from common.mysql import MySql


class Autohome(Resource):
    todos = ['guide_price', 'brand_name', 'distinct-query-all', 'distinct-query-equal', 'distinct-query-in']

    def abort_if_todo_doesnt_exist(self, todo):
        if todo not in self.todos:
            abort(404, message="Todo {} doesn't exist".format(todo))

    def post(self, todo):
        self.abort_if_todo_doesnt_exist(todo)
        parser = reqparse.RequestParser()
        db = MySql('crawler')
        if todo == self.todos[0]:
            parser.add_argument('b_name', type=str)
            parser.add_argument('m_name', type=str)
            args = parser.parse_args()
            b_name = args['b_name']
            m_name = args['m_name']
            sql = '''
                SELECT
                series_name,
                max(cast(replace(guide_price,'万','')AS DECIMAL(10, 2)))AS maxprice,
                min(cast(replace(guide_price,'万','')AS DECIMAL(10, 2)))AS minprice
                FROM autohome 
                WHERE brand_name = '%s'
                AND engine_info NOT LIKE '%%款'
                AND main_level = '%s'
                AND guide_price <> '暂无报价'
                GROUP BY series_name
                ORDER BY minprice
            '''
            parameters = (b_name, m_name,)
            result = []
            for item in db.query(sql % parameters):
                result.append(dict(s_name=item[0], max_p=str(item[1]), min_p=str(item[2])))
            return result, 200

        elif todo == self.todos[1]:
            parser.add_argument('b_name', type=str)
            args = parser.parse_args()
            b_name = args['b_name']
            sql = '''
                SELECT
                DISTINCT brand_code,`brand_name`
                FROM
                `autohome`
                WHERE
                upper(brand_name) LIKE '%%%s%%'
                ORDER BY brand_name
            '''
            parameters = (b_name.upper(),)
            result = []
            for item in db.query(sql % parameters):
                result.append(dict(b_code=item[0], b_name=str(item[1])))
            return result, 200

        elif todo == self.todos[2]:
            parser.add_argument("sel_col", type=str)
            args = parser.parse_args()
            sel_col = args['sel_col']
            sql = '''
                SELECT
                DISTINCT %s
                FROM
                `autohome`
            '''
            parameters = (sel_col,)
            result = []
            for item in db.query(sql % parameters):
                result.append(dict(result=item[0]))
            return result, 200

        elif todo == self.todos[3]:
            parser.add_argument("sel_col", type=str)
            parser.add_argument("where_col", type=str)
            parser.add_argument("query_col", type=str)
            args = parser.parse_args()
            sel_col = args['sel_col']
            where_col = args['where_col']
            query_col = args['query_col']
            sql = '''
                SELECT
                DISTINCT %s
                FROM
                `autohome`
                WHERE
                %s = '%s'
            '''
            parameters = (sel_col,where_col,query_col)
            result = []
            for item in db.query(sql % parameters):
                result.append(dict(result=item[0]))
            return result, 200

        elif todo == self.todos[2]:
            parser.add_argument("sel_col", type=str)
            parser.add_argument("where_col", type=str)
            parser.add_argument("query_col", type=str)
            args = parser.parse_args()
            sel_col = args['sel_col']
            where_col = args['where_col']
            query_col = args['query_col']
            sql = '''
                SELECT
                DISTINCT %s
                FROM
                `autohome`
                WHERE
                %s IN (%s)
            '''
            parameters = (sel_col,where_col,query_col)
            result = []
            for item in db.query(sql % parameters):
                result.append(dict(result=item[0]))
            return result, 200
